import java.sql.*;

/**
 * Keeps track of important records of the patient
 */
public class VitalSigns {
	private int treatmentRecordId;
	private int bloodPressure;
	private int pulse;
	private int temperature;
	private int height;
	private int weight;

	/**
	 * Initializes all the tests from a patient's physical checkup
	 * 
	 * @param record
	 *            in the form of a treatment record
	 */
	public VitalSigns(int recordId) {
		this.treatmentRecordId = recordId;
		bloodPressure = 0;
		pulse = 0;
		temperature = 0;
		height = 0;
		weight = 0;
	}

	/**
	 * gets the treatment record of the patient which includes any past
	 * prescribed drugs, or anything else relevant to the patient
	 * 
	 * @return treatment record in the form of a treatment record
	 */
	public TreatmentRecord getTreatmentRecord() {
		return TreatmentRecord.readTreatmentRecord(treatmentRecordId);
	}

	/**
	 * sets the treatment record for the patient
	 * 
	 * @param record
	 *            in the form of a treatment record
	 */
	public void setTreatmentRecord(int recordId) {
		this.treatmentRecordId = recordId;
	}

	/**
	 * getter for blood pressure of patient
	 * 
	 * @return- blood pressure in form of an integer
	 */
	public int getBloodPressure() {
		return bloodPressure;
	}

	/**
	 * setter for blood pressure of patient
	 * 
	 * @param pressure
	 *            in form of an integer
	 * 
	 */
	public void setBloodPressure(int pressure) {
		this.bloodPressure = pressure;
	}

	/**
	 * getter for pulse of patient
	 * 
	 * @return pulse in form of an integer
	 */
	public int getPulse() {
		return pulse;
	}

	/**
	 * setter for pulse of patient
	 * 
	 * @param pulse
	 *            in form of an integer
	 */
	public void setPulse(int pulse) {
		this.pulse = pulse;
	}

	/**
	 * getter for temperature of patient
	 * 
	 * @return temperature in form of an integer
	 */
	public int getTemperature() {
		return temperature;
	}

	/*
	 * setter for temperature of patient
	 * 
	 * @param temperature in form of an integer
	 */
	public void setTemperature(int temperature) {
		this.temperature = temperature;
	}

	/*
	 * getter for height
	 * 
	 * @return height in form of an integer
	 */
	public int getHeight() {
		return height;
	}

	/**
	 * setter for height
	 * 
	 * @param height
	 *            in form of an integer
	 */

	public void setHeight(int height) {
		this.height = height;
	}

	/**
	 * getter for weight
	 * 
	 * @return weight in form of an integer
	 */
	public int getWeight() {
		return weight;
	}

	/**
	 * setter for weight
	 * 
	 * @param weight
	 *            in form of an integer
	 */
	public void setWeight(int weight) {
		this.weight = weight;
	}

	// CRUD functionality

	// Create
	public static boolean createVitalSigns(String username, int recordId,
			int pressure, int pulse, int temp, int height, int weight) {
		String sql = "INSERT INTO VitalSigns VALUES (username='" + username
				+ "', treatment_record=" + recordId + ", blood_pressure="
				+ pressure + ", pulse=" + pulse + ", temperature=" + temp
				+ ", height=" + height + ", weight=" + weight + ")";
		int rows = Database.query(sql);
		if (rows == 1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	// Read
	public static VitalSigns readVitalSigns(String username, int recordId) {
		String sql = "SELECT * FROM VitalSigns WHERE username='" + username
				+ "' AND treatment_record=" + recordId;
		ResultSet rs = Database.selectQuery(sql);
		try {
			while (rs.next()) {
				int pressure = rs.getInt("blood_pressure");
				int pulse = rs.getInt("pulse");
				int temp = rs.getInt("temperature");
				int height = rs.getInt("height");
				int weight = rs.getInt("weight");
				VitalSigns signs = new VitalSigns(recordId);
				signs.setBloodPressure(pressure);
				signs.setPulse(pulse);
				signs.setTemperature(temp);
				signs.setHeight(height);
				signs.setWeight(weight);
				return signs;
			}
		} catch (Exception e) {
		}
		return null;
	}

	// Update
	public static boolean updateVitalSigns(String username, int recordId,
			int pressure, int pulse, int temp, int height, int weight) {
		String sql = "UPDATE VitalSigns SET blood_pressure=" + pressure
				+ ", pulse=" + pulse + ", temperature=" + temp + ", height="
				+ height + ", weight=" + weight + " WHERE username='"
				+ username + "' AND treatment_record=" + recordId;
		int rows = Database.query(sql);
		if (rows == 1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	// Delete
	public static boolean deleteVitalSigns(String username, int recordId) {
		String sql = "DELETE FROM VitalSigns WHERE username='" + username
				+ "' AND treatment_record=" + recordId;
		int rows = Database.query(sql);
		if (rows == 1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}
